package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.huanghefd.controller.BasedataController;
import com.ruoyi.huanghefd.domain.Dep;
import com.ruoyi.huanghefd.domain.Sysuserfd;
import com.ruoyi.huanghefd.mapper.DepMapper;
import com.ruoyi.huanghefd.mapper.SysuserfdMapper;
import com.ruoyi.huanghefd.service.BasedataService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author gnplum
 * @Date 2023/11/1 16:33
 * @PackageName:com.ruoyi.huanghefd.service.impl
 * @ClassName: BasedataServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class BasedataServiceImpl extends ServiceImpl<DepMapper, Dep>
        implements BasedataService {
    private static final Logger log = LoggerFactory.getLogger(BasedataServiceImpl.class);

    @Resource
    DepMapper depMapper;

    @Resource
    SysuserfdMapper sysuserfdMapper;

    @Resource
    private SysUserMapper userMapper;

    @Override
    public AjaxResult queryDeps(String depId) {
        try {
            List<Dep> depList = depMapper.queryDepList(depId);
            return AjaxResult.success("查询部门列表成功", depList);
        } catch (Exception e) {
            return AjaxResult.error("查询部门列表失败", e);
        }
    }

    @Override
    public AjaxResult querySysuserfds(String userId,String userName,String depId) {
        try {
            List<Sysuserfd> sysuserfdList = sysuserfdMapper.querySysuserfdList(userId, userName, depId);
            return AjaxResult.success("查询用户列表成功", sysuserfdList);
        } catch (Exception e) {
            return AjaxResult.error("查询用户列表失败", e);
        }
    }

    @Override
    public AjaxResult verifySignByPsw(String userId, String password) {
        try {
            //获取前端勾选用户登陆密码
            String encodedPassword = userMapper.getPsw(userId);
            log.info("登录密码=="+encodedPassword);
            //加密签名密码
/*            String encodePswBySign= SecurityUtils.encryptPassword(password);
            log.info("签名密码加密=="+encodePswBySign);*/
            //对比签名是否正确
           Boolean result = SecurityUtils.matchesPassword(password,encodedPassword);
           if(!result){
               return AjaxResult.error("密码校验失败");
           }
        } catch (Exception e) {
            return AjaxResult.error(e.getCause() + e.getMessage());
        }
        return AjaxResult.success("密码校验正确");
    }
}
